Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stb_image: fix implicit conversion warnings #1443

Closed
wants to merge 2 commits into from
Closed

stb_image: fix implicit conversion warnings #1443

wants to merge 2 commits into from

Conversation

diamante0018
Copy link

@diamante0018 diamante0018 commented Jan 30, 2023

Hello, first-time contributor here.
This pull request fixes 3 warnings that I get when using this library on the MSVC platform.
I have set my MSVC compiler to treat these warnings as errors so this is necessary for me to continue using this library without making any changes to the solution file on visual studio/turning off warnings.
Thanks.

Closes #1444

@nothings
Copy link
Owner

None of these seem like the correct way to fix the issue. For example, for security reasons, the code wants to check that multiplying two shorts gives a valid result. If one of the inputs isn't a short, but you cast it to a short for just that function call, you could truncate it smaller than it is, the security test passed, but then the actual multiply is done with a larger number, breaking the security check.

@diamante0018
Copy link
Author

diamante0018 commented Jan 30, 2023

So, if I understand this correctly, instead of casting you would prefer I clamp the value to be in between short min/max for that function call
(And unsigned char for the final warning)?
Is there a place in this repository where a similar thing like this is done already so I may have a look at it?

@nothings
Copy link
Owner

I'm pretty much saying that @rygorous (who wrote this code) should take a look at it and figure out what's going on. Presumably either dc ought to be stored in a short, or stbi__mul2shorts_valid should take ints, or something.

@diamante0018
Copy link
Author

diamante0018 commented Jan 30, 2023

Roger, feel free to close this pr when it is fixed properly or I will come back to it in a bit to see when it has been fixed to close it myself 🙂
I don't think I'm qualified enough to fix them myself but if the commit author wants to suggest anything I can do, I will try.

@rygorous
Copy link
Collaborator

I didn't write it, Neil did, but I'll have a look later.

stb_image.h Outdated
@@ -2221,7 +2221,7 @@ static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman
if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta","Corrupt JPEG");
dc = j->img_comp[b].dc_pred + diff;
j->img_comp[b].dc_pred = dc;
if (!stbi__mul2shorts_valid(dc, dequant[0])) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
if (!stbi__mul2shorts_valid((short) dc, dequant[0])) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yep, this was my fault in #1297 ! Thank you for the fix. How about checking that dc is valid to cast to a short, and casting the `stbi__uint16 as well, like this?

if ((dc > SHRT_MAX) || (dequant[0] > SHRT_MAX) || !stbi__mul2shorts_valid((short) dc, (short)dequant[0])) return stbi__err("can't merge dc and ac", "Corrupt JPEG");

stb_image.h Outdated
@@ -2278,7 +2278,7 @@ static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__
if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta", "Corrupt JPEG");
dc = j->img_comp[b].dc_pred + diff;
j->img_comp[b].dc_pred = dc;
if (!stbi__mul2shorts_valid(dc, 1 << j->succ_low)) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
if (!stbi__mul2shorts_valid((short) dc, 1 << j->succ_low)) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, maybe this could check as well that dc can be casted to a short before doing so, like this?

if ((dc > SHRT_MAX) || !stbi__mul2shorts_valid((short) dc, 1 << j->succ_low)) return stbi__err("can't merge dc and ac", "Corrupt JPEG");

@diamante0018
Copy link
Author

Thanks for the feedback, let me know if the last cast to unsigned char is fine.
Also I'm not familiar with the code style of this project but I tried my best to follow it, please let me know if it is okay.

@NBickford-NV
Copy link
Contributor

Thank you, that looks good to me from a security perspective - stbi__skip_jpeg_junk_at_end() always returns a value between 0 and 0xff inclusive, so another solution is it could return a stbi_uc instead, which would avoid the cast to unsigned char.

By the way, Sean and Fabian, please feel free to @ me any time - I'm always happy to help with things!

@diamante0018
Copy link
Author

You are right! I changed the return type of stbi__skip_jpeg_junk_at_end and removed the cast entirely.
Thanks.

Copy link
Contributor

@NBickford-NV NBickford-NV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good to me!

Ah, I don't have permission to merge anything in, though, and am not a project member, sorry - that would be @nothings and @rygorous . I understand it usually takes some time for things to be merged in - what I've usually done in the past for projects that require a patched version of stb_image is I've pointed Git submodules to the fork used for the merge request (or patched the copy if the project is using a copy of the file), then pointed them back to nothings:master once the patch has been merged.

@diamante0018
Copy link
Author

Thanks for the tip, I will do that I think. Or just suppress the warning before including the headers.

NBickford-NV added a commit to NBickford-NV/tinygltf that referenced this pull request Mar 29, 2023
@ChrisThrasher
Copy link

I'd like to bump this PR since I'm dealing with the same problems in #1444. @nothings Is this good to merge?

@diamante0018
Copy link
Author

Woot

@rygorous
Copy link
Collaborator

A fix for this is already in the dev branch (and has been since May 2nd, commit 8c3aa05), it's just waiting on the next time we do a release.

@rygorous rygorous closed this Jun 19, 2023
@diamante0018 diamante0018 deleted the fix/warnings branch June 19, 2023 08:14
@diamante0018
Copy link
Author

diamante0018 commented Jun 19, 2023

Better late then never. May I suggest that some MSVC tests are added to STB workflow(s) to see if it compiles with all warnings enabled.
When I tried to compile stb (a week ago or so) it still failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stb_image: warnings MSVC: Implicity conversions/possible loss of data
5 participants